import {
  getLabelList,
  fetchUpOrDownLabel,
  fetchRemoveLabel,
  fetchEditLabel,
  fetchAddLabel,
  fetchLabelInfoById,
  fetchHotQuestionList,
  doRelatedDataByLabelId,
  fetchSaveOrder,
  fetchRemoveQuestion
} from '@/services/hotQuestion'

export default {
  namespace: 'hotQuestion',
  state: {
    data: {
      qaList: [],
      total: 0,
      suggestList: [],
    },
  },

  effects: {
    *fetchLabelList({payload,callback}, { call, put }) {
      const response = yield call(getLabelList,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doUpOrDownLabel({payload,callback}, { call, put }) {
      const response = yield call(fetchUpOrDownLabel,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doRemoveLabel({payload,callback}, { call, put }) {
      const response = yield call(fetchRemoveLabel,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doEditLabel({payload,callback}, { call, put }) {
      const response = yield call(fetchEditLabel,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doAddLabel({payload,callback}, { call, put }) {
      const response = yield call(fetchAddLabel,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getLabelInfoById({payload,callback}, { call, put }) {
      const response = yield call(fetchLabelInfoById,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getHotQuestionList({payload,callback}, { call, put }) {
      const response = yield call(fetchHotQuestionList,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doRelatedData({payload,callback}, { call, put }) {
      const response = yield call(doRelatedDataByLabelId,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doSaveOrder({payload,callback}, { call, put }) {
      const response = yield call(fetchSaveOrder,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *doRemoveQuestion({payload,callback}, { call, put }) {
      const response = yield call(fetchRemoveQuestion,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
